﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace System
{
	public enum PointPlot
	{
		SinglePoint,
		Crosshair
	}
	public class MathDrawer
	{
		public Bitmap BMP;
		Graphics G;
		public MathDrawer(int W, int H)
		{
			BMP = new Bitmap(W, H);
			G = Graphics.FromImage(BMP);
			G.Clear(Color.White);
		}
		V3D Center;
		V3G EarthCenter;
		double scale;
		public void Set3D(V3D P, double sc)
		{
			Center = P;
			scale = sc;
		}
		public void Set3G(double lng, double lat, double sc)
		{
			EarthCenter = new V3G(lng, lat);
			Center = new V3D(BMP.Width * 0.5, BMP.Height * 0.5);
			scale = sc;
		}
		/// <summary>
		/// Draw Points to canvas
		/// </summary>
		/// <param name="Point">Point (X,Y used)</param>
		/// <param name="Size">plot size</param>
		/// <param name="plot">plot type (default: point)</param>
		V3D Get3D(V3D P)
		{
			return new V3D(Center.X + P.X / scale * Math.Max(BMP.Width / 2, BMP.Height / 2), Center.Y - P.Y / scale * Math.Max(BMP.Width / 2, BMP.Height / 2));
		}
		V3D Get3G(V3G P)
		{
			return Get3D(EarthCenter.GetLocalDirection(P));
		}
		public void DrawPoint(V3D Point, int Size, PointPlot plot = PointPlot.SinglePoint)
		{
			V3D P = Get3D(Point);
			if (plot == PointPlot.SinglePoint)
				G.FillRectangle(Brushes.Black, (int)P.X - Size / 2, (int)P.Y - Size / 2, Size, Size);
		}
		public void DrawPoint(V3G Point, int Size, Color col, PointPlot plot = PointPlot.SinglePoint)
		{
			V3D P = Get3G(Point);
			if (plot == PointPlot.SinglePoint)
				G.FillRectangle(new SolidBrush(col), (int)P.X - Size / 2, (int)P.Y - Size / 2, Size, Size);
		}

		public void DrawPolygon(V3G[] Pts, Color C)
		{
			PointF[] X = new PointF[Pts.Length];
			for (int i = 0; i < Pts.Length; i++)
				X[i] = Get3G(Pts[i]).ToPointF();
			G.FillPolygon(new SolidBrush(C), X);
			G.DrawPolygon(Pens.Black, X);
		}
		public void DrawLine(V3G[] Pts, Color C)
		{
			PointF[] X = new PointF[Pts.Length];
			for (int i = 0; i < Pts.Length; i++)
				X[i] = Get3G(Pts[i]).ToPointF();
			G.DrawLines(new Pen(C), X);
		}
	}
}